package com.wanxi.dao.Impl;

import com.wanxi.dao.CompanyDao;
import com.wanxi.model.Company;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;

public class CompanyDaoImpl implements CompanyDao {
    @Override
    public Company findCompanyById(Company company) {
        String sql="select * from company where id="+company.getId();
        ResultSet query = JDBC.query(sql);
        try{
            while (query.next()){
                Company c=new Company();
                c.setId(query.getInt("id"));
                c.setLogoLocation(query.getString("logoLocation"));
                c.setChineseName(query.getString("chineseName"));
                c.setEnglishName(query.getString("englishName"));
                c.setAddress(query.getString("address"));
                c.setIntroduction(query.getString("introduction"));
                c.setQqID(query.getString("qqID"));
                c.setWbID(query.getString("wbID"));
                c.setTelephone(query.getString("telephone"));
                c.setEmail(query.getString("email"));
                c.setWeChat(query.getString("weChat"));
                c.setEnable(query.getInt("enable"));
                c.setCreateTime(query.getTimestamp("createTime"));
                c.setUpdateTime(query.getTimestamp("updateTime"));
                c.setRemark(query.getString("remark"));
                return c;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public int update(Company company) {
        String sql="update company set logoLocation='"+company.getLogoLocation()
                +"',chineseName='"+company.getChineseName()
                +"',englishName='"+company.getEnglishName()
                +"',introduction='"+company.getIntroduction()
                +"',qqID='"+company.getQqID()
                +"',wbID='"+company.getWbID()
                +"',telephone='"+company.getTelephone()
                +"',email='"+company.getEmail()
                +"',weChat='"+company.getWeChat()
                +"',address='"+company.getAddress()
                +"',createTime=(select now()),updateTime=(select now()),remark='"+company.getRemark()
                +"' where id=1";
        return JDBC.update(sql);
    }
}
